package com.catalog.Manager
{
	import com.catalog.ValueObject.PicturePageVO;
	import com.catalog.ValueObject.PictureVO;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;

	[Event(name="LoadPictureModuleConfigComplete",type="flash.events.Event")]
	public class PictureModuleManager extends EventDispatcher
	{
		public static const PICTURE_PAGE_CONFIG_URL:String = "Resource/config/PicturePageConfig.xml";
		/**
		 * 存放用户上传的图片的集合 
		 */		
		[Bindable]
		public var pictureCartList:ArrayCollection = new ArrayCollection();
		
		/**
		 * 存放配置文件信息的集合
		 */		
		[Bindable]
		public var pictureModuleBarList:ArrayCollection = new ArrayCollection();
		
		public function PictureModuleManager()
		{
			super();
			getPicturePageConfig();
			setDefaultPicInCart();
		}
		
		private static var _instance:PictureModuleManager;
		public static function getInstance():PictureModuleManager
		{
			if( !_instance )
				_instance = new PictureModuleManager();
			return _instance;
		}
		
		/**
		 * 获取图片编辑模块配置文件 
		 * 
		 */		
		private function getPicturePageConfig():void
		{
			var httpService:HTTPService = new HTTPService();
			httpService.addEventListener(ResultEvent.RESULT,onPicturePageConfigResultHandler );
			httpService.resultFormat = "e4x";
			httpService.url = PICTURE_PAGE_CONFIG_URL;
			httpService.send();
		}
		
		private function onPicturePageConfigResultHandler( event:ResultEvent ):void
		{
			var resultXml:XML = event.result as XML;
			for each( var item:XML in resultXml.Page )
			{
				var page:PicturePageVO = new PicturePageVO();
				page.label = item.@label;
				page.className = item.@className;
				page.tooltip = item.@tooltip;
				pictureModuleBarList.addItem( page );
			}
			pictureModuleBarList.refresh();
			dispatchEvent(new Event("LoadPictureModuleConfigComplete"));
		}
		
		/**为用户提供一张默认的图片*/
		private function setDefaultPicInCart():void
		{
			var pictureVo:PictureVO = new PictureVO();
			pictureVo.name = "默认图片";
			pictureVo.url = "Resource/testImage1.jpg";
			pictureCartList.addItem( pictureVo );
			pictureCartList.refresh();
		}
	}
}